const _ = wx.cloud.database().command
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: '',
        userimg: '',
        list: '',
        phone: '',
        isShow: false,
        isLike: false,
    },
    urlnew: function (event) {
        // console.log(event);
        let id = event.currentTarget.dataset.id
        console.log(id);
        wx.navigateTo({
            url: '/mypages/news/news?titid=' + id,
            //   events: events,
            success: (result) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },
    randomlist: function (arr) {
        let arry1 = [];
        arry1 = arr.map(item => {
            return { name: item.name, userimg: item.userimg, phone: item.phone }
        })
        let arry2 = arr.map(item => {
            return item.issue
        })
        let arry3 = []
        for (var i = 0; i < arry1.length; i++) {
            for (var j = 0; j < arry2.length; j++) {
                for (var k = 0; k < arry2[j].length; k++) {
                    if (arry1[i].phone == arry2[j][k].phone) {
                        arry2[j][k].name = arry1[i].name;
                        arry2[j][k].userimg = arry1[i].userimg;
                    }
                }
            }
        }
        for (var z = 0; z < arry2.length; z++) {
            arry3 = arry3.concat(arry2[z])

        }
        let arry4 = arry3.slice(0);
        let temp;
        for (let n = 0; n < arry4.length / 2; n++) {
            temp = arry4[n];
            arry4[n] = arry4[arry4.length - 1 - n];
            arry4[arry4.length - 1 - n] = temp;
        }
        // console.log(arry1);
        // console.log(arry2);
        console.log(arry3);
        console.log(arry4);
        this.setData({
            lilist: arry3,
            lilist2: arry4
        })
    },
    conshow: function () {
        if (this.data.isShow == true) {
            this.setData({
                isShow: false
            })
        } else {
            this.setData({
                isShow: true
            })
        }
    },
    //打开导航
    searchmap: function () {
        let latitude = this.data.list.latitude;
        let longitude = this.data.list.longitude;
        let name = this.data.list.addressname;
        let address = this.data.list.address
        wx.openLocation({
            latitude: latitude,
            longitude: longitude,
            name: name,
            address: address
        })
    },
    //收藏
    coll: async function () {
        let user = wx.getStorageSync('user');
        if (!user) {
            wx.showToast({
                title: '未登录',
                icon: 'error',
                duration: 2000
            })
            return
        }
        let phone = this.data.phone;
        let titid = this.data.list.titid;
        let that = this
        if (this.data.isLike == true) {
            this.setData({
                isLike: false
            });
            await wx.cloud.database().collection('my_user').where({
                phone: phone
            }).update({
                data: {
                    collections: _.pull(
                        { titid: _.eq(titid) }
                    )
                },
                success(res) {
                    console.log("取消收藏");
                },
                fail(res) {
                    console.log("取消失败", res);
                }

            })
        } else {
            this.setData({
                isLike: true
            });
            await wx.cloud.database().collection('my_user').where({
                phone: phone
            }).update({
                data: {
                    collections: _.push(
                        { titid: titid }
                    )
                },
                success(res) {
                    console.log("收藏成功");
                },
                fail(res) {
                    console.log("收藏失败", res);
                }

            })

        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.cloud.callFunction({
            name: 'home',
            // config: {
            //   env: 'cloud1-0gynot18c71f42a8'
            // },
            data: {
                type: 'share'
            }
        }).then((resp) => {
            console.log(resp.result);
            this.randomlist(resp.result.data);

        }).catch((e) => {
            console.log(e);

        });

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var pages = getCurrentPages(); 				//获取加载的页面
        var currentPage = pages[pages.length - 1] 	//获取当前页面的对象
        const options = currentPage.options 		//获取当前页面的参数
        console.log(options); //打印当前页面的参数
        let that = this;
        // console.log(options);
        wx.cloud.database().collection('my_share').where({
            issue: _.elemMatch({ titid: _.eq(options.titid) }),
        }).get({
            success(res) {
                let arr = ''
                res.data[0].issue.forEach(element => {
                    if (element.titid == options.titid) {
                        arr = element
                    }
                });
                console.log(arr);
                console.log("获取数据成功", res.data[0]);
                that.setData({
                    list: arr,
                    name: res.data[0].name,
                    userimg: res.data[0].userimg,
                    // phone: res.data[0].phone,
                })
            },
            fail(res) {
                console.log("获取数据失败", res)
            }
        })
        let user = wx.getStorageSync('user')
        if (user) {
            this.setData({
                phone: user.phone
            })
        };
        let phone = this.data.phone
        wx.cloud.database().collection('my_user').where({
            phone: phone
        }).get({
            success(res) {
                console.log("获取成功", res);
                let arr = res.data[0].collections;
                let id = '';
                for(var i=0;i<arr.length;i++){
                    if(arr[i].titid==options.titid){
                        id = arr[i]
                    }
                }
                console.log(id);
                if (id && id.titid == options.titid) {
                    that.setData({
                        isLike: true
                    })
                } else {
                    that.setData({
                        isLike: false
                    })
                }
            },
            fail(res) {
                console.log("获取失败", res);
            }
        })
    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})